<?php
//powered by chinieer for cherishing the memory of geopolitics database
require_once('site-config.php');

class mysql{
  private $mysql_host;
  private $mysql_user;
  private $mysql_password;
  private $mysql_database;
  private $mysql_ut;
  private $mysql_table;
 
  
  function __construct(){
    $this->mysql_host = HOST;
    $this->mysql_user = USER;
    $this->mysql_password = PSW;
    $this->mysql_database = DBN;
    $this->mysql_ut = UT;
    $this->mysql_table = TABLE;
   	    
    $this->connect();   
  }
  function connect(){
    $link=mysql_connect($this->mysql_host,$this->mysql_user,$this->mysql_password) or die ($this->error());
    mysql_select_db($this->mysql_database,$link) or die("没该数据库：".$this->table);
    mysql_query("SET NAMES '$this->mysql_ut'");
  }
 
  function query($sql, $type = '') {
    if(!($query = mysql_query($sql))) 
      $this->show('Say:', $sql);       
    return $query;
  }
  
  function show($message = '', $sql = '') {
    if(!$sql) echo $message;
    else echo $message.'<br>'.$sql;
  } 
  
  function fn_select_type($movietype,$page,$ColNum){  	 
  	$begin = $ColNum*($page-1);  	   
    return $this->query("select * from $this->mysql_table  where movietype like '%$movietype%' LIMIT $begin,$ColNum");             
  }
  function fn_select_type_count($movietype=null){
  	if($movietype===null){
  	  $query = $this->query("select id from $this->mysql_table");
  	}
  	else{
  		$query = $this->query("select id from $this->mysql_table  where movietype like '%$movietype%'");
  	} 
  	return $this->num_rows($query);   	                
  }  


 
  function fn_select_all($moiveid=null){
  	if($moiveid==null){  	   	 	   
     return $this->query("select * from $this->mysql_table");      
    }
    else{
     return $this->query("select * from $this->mysql_table where id=$moiveid");   
    }            
  } 
  function fn_update_initial($moiveid,$initial){  	   	 	   
    return $this->query("update $this->mysql_table set initial='$initial' where id=$moiveid");             
  }  
  function fn_update_newstime($moiveid,$newstime){  	   	 	   
    return $this->query("update $this->mysql_table set newstime='$newstime' where id=$moiveid");             
  }  
  
  function num_rows($query) {
    return @mysql_num_rows($query);
  }     

  function fetch_assoc($query) {
    return mysql_fetch_assoc($query);
  }
  
  
  //取首字母.但是有个别情况转化不了
  function getfirstchar($s0){   
   if(ord(strtolower($s0)) >= ord("a") and ord(strtolower($s0)) <= ord("z")){
      return strtoupper($s0[0]);
   }
   $s = iconv("UTF-8", "gb2312//IGNORE", $s0); // 不要转换成GB2312内没有的字符哦，^_^
   $asc = @ord($s{0}) * 256 + @ord($s{1})-65536;
   if($asc >= -20319 and $asc <= -20284)return "A";
   if($asc >= -20283 and $asc <= -19776)return "B";
   if($asc >= -19775 and $asc <= -19219)return "C";
   if($asc >= -19218 and $asc <= -18711)return "D";
   if($asc >= -18710 and $asc <= -18527)return "E";
   if($asc >= -18526 and $asc <= -18240)return "F";
   if($asc >= -18239 and $asc <= -17923)return "G";
   if($asc >= -17922 and $asc <= -17418)return "H";
   if($asc >= -17417 and $asc <= -16475)return "J";
   if($asc >= -16474 and $asc <= -16213)return "K";
   if($asc >= -16212 and $asc <= -15641)return "L";
   if($asc >= -15640 and $asc <= -15166)return "M";
   if($asc >= -15165 and $asc <= -14923)return "N";
   if($asc >= -14922 and $asc <= -14915)return "O";
   if($asc >= -14914 and $asc <= -14631)return "P";
   if($asc >= -14630 and $asc <= -14150)return "Q";
   if($asc >= -14149 and $asc <= -14091)return "R";
   if($asc >= -14090 and $asc <= -13319)return "S";
   if($asc >= -13318 and $asc <= -12839)return "T";
   if($asc >= -12838 and $asc <= -12557)return "W";
   if($asc >= -12556 and $asc <= -11848)return "X";
   if($asc >= -11847 and $asc <= -11056)return "Y";
   if($asc >= -11055 and $asc <= -10247)return "Z";
   return NULL; // 返回原字符，不作转换。（标点、空格、繁体字都会直接返回）
  }

} 